package com.dranie.common;

import com.demo.blog.Blog;
import com.demo.blog.BlogController;
import com.dranie.action.*;
import com.dranie.entity.Category;
import com.dranie.entity.Goods;
import com.dranie.entity.Order;
import com.dranie.entity.User;
import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.core.JFinal;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.c3p0.C3p0Plugin;
import com.jfinal.render.ViewType;

/**
 * API引导式配置
 */
public class DranieConfig extends JFinalConfig {

    /**
     * 建议使用 JFinal 手册推荐的方式启动项目 运行此 main
     * 方法可以启动项目，此main方法可以放置在任意的Class类定义中，不一定要放于此
     */
    public static void main(String[] args) {
        System.out.println(DranieConfig.class.getResource("/"));
        JFinal.start("webapp", 8888, "/", 5);
    }

    /**
     * 配置常量
     */
    public void configConstant(Constants me) {
        // 加载少量必要配置，随后可用getProperty(...)获取值
        loadPropertyFile("a_little_config.txt");
        me.setDevMode(getPropertyToBoolean("devMode", false));
//        me.setViewType(ViewType.JSP);
    }

    /**
     * 配置路由
     */
    public void configRoute(Routes me) {
        //me.add("/", CommonController.class);
       // me.add("/blog", BlogController.class);
        me.add("/admin", AdminController.class);
        me.add("/admin/user", UserController.class);
        me.add("/admin/goods", GoodsController.class);
        me.add("/admin/order", OrderController.class);
        me.add("/admin/category", CategoryController.class);
        me.add("/web", WebController.class);
    }

    /**
     * 配置插件
     */
    public void configPlugin(Plugins me) {
        // 配置C3p0数据库连接池插件
        C3p0Plugin c3p0Plugin = new C3p0Plugin(getProperty("jdbcUrl"),
                getProperty("user"), getProperty("password").trim());
        me.add(c3p0Plugin);

        // 配置ActiveRecord插件
        ActiveRecordPlugin arp = new ActiveRecordPlugin(c3p0Plugin);
        me.add(arp);
        arp.addMapping("blog", Blog.class); // 映射blog 表到 Blog模型
        arp.addMapping("order", Order.class);
        arp.addMapping("goods", Goods.class);
        arp.addMapping("category", Category.class);
        arp.addMapping("user", User.class);
    }

    /**
     * 配置全局拦截器
     */
    public void configInterceptor(Interceptors me) {

    }

    /**
     * 配置处理器
     */
    public void configHandler(Handlers me) {

    }
}
